package com.sc.socket.chatroom.client;

import com.sc.socket.common.CloseUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
 * @author shuc001
 * @Date 2019-02-05 14:22
 */
public class TCPClient {
    private final Socket socket;
    private final ReadHandler readHandler;
    private final PrintStream printStream;
    private final String tcpInfo;

    public TCPClient(Socket socket, ReadHandler readHandler) throws IOException {
        this.socket = socket;
        this.readHandler = readHandler;
        this.printStream = new PrintStream(socket.getOutputStream());
        this.tcpInfo = "C=[A:"+socket.getLocalAddress()+"P:"+socket.getLocalPort()+"]"+"" +
                "S=[A:"+socket.getInetAddress()+"P:"+socket.getPort()+"]";
    }
    public void exit(){
        readHandler.exit();
        CloseUtils.close(printStream);
        CloseUtils.close(socket);
    }

    public void send(String msg){
        printStream.println(msg);
    }

    public static TCPClient startWith(ServerInfo serverInfo) throws IOException {
        Socket socket = new Socket();
        socket.setSoTimeout(3000);
        socket.connect(new InetSocketAddress(Inet4Address.getByName(serverInfo.getAddress()),
                serverInfo.getPort()),2000);
        try{
            ReadHandler readHandler = new ReadHandler(socket.getInputStream());
            readHandler.start();
            return new TCPClient(socket,readHandler);
        }catch (Exception e){
            System.out.println("连接异常");
            CloseUtils.close(socket);
        }
        return null;
    }

    public String getTcpInfo() {
        return tcpInfo;
    }

    static class ReadHandler extends Thread{
        private boolean done = false;
        private final InputStream inputStream;

        ReadHandler(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        @Override
        public void run() {
            try{
                BufferedReader input = new BufferedReader(new InputStreamReader(
                        inputStream));
                do{
                    String str;
                    try {
                        str = input.readLine();
                    }catch (SocketTimeoutException e){
                        continue;
                    }
                    if(str==null){
                        System.out.println("已断开连接");
                        break;
                    }
                    System.out.println(str);
                }while (!done);
            }catch (Exception e){
                if(!done){
                    System.out.println("客户端异常退出");
                }
            }finally {
                CloseUtils.close(inputStream);
            }
        }

        void exit(){
            done=true;
            CloseUtils.close(inputStream);
        }
    }




}
